//构造函数
function Router() {
  this.routers = {};
  this.currentUrl = "";
}

Router.prototype = {
  //init 监听浏览器url hash更新事件
  init: function () {
    window.addEventListener("load", this.refresh.bind(this), false);
    window.addEventListener('hashchange', this.refresh.bind(this), false);
  },
  //route 存储路由更新时的回调到回调数组routes中，回调函数将负责对页面的更新
  router: function (path, cb) {
    this.routers[path] = cb || function () {};
  },
  //refresh 执行当前url对应的回调函数，更新页面
  refresh: function () {
    this.currentUrl = location.hash.slice(1) || "/";
    this.routers[this.currentUrl]();
  }
};

//给window对象挂载属性
window.Router = new Router();
window.Router.init();